package no.ntnu.fp.model;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public abstract class Entity {
	protected long id;
	
	public Entity() { }
	
	public Entity(Element rootNode) {
		initializeFromXml(rootNode);
	}

	public Entity(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}
	
	@Override
	public String toString() {
		return String.format("%s#%d", getClass().getSimpleName(), getId());
	}
	
	public abstract Element toXml(Document dom);
	protected abstract void initializeFromXml(Element rootNode);

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Entity other = (Entity) obj;
		if (id != other.id)
			return false;
		return true;
	}
}
